import React, {Component} from 'react';
import {withRouter,Link} from 'react-router-dom'
import u from '../../common.js'
import { Input, message } from 'antd';

import "./../../static/personal/personalInfo.styl"

export default class PersonalInfo extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      oldPassword: ''
    }
  }

  componentWillMount() {
    let password = localStorage.getItem('password')
    this.setState({
      oldPassword: password
    })
  }

  onChangePsd() {
    let username = localStorage.getItem('username')
    let password = localStorage.getItem('password')
    let newPassword = document.getElementById('user').value
    console.log(password, typeof password)
    console.log(newPassword, typeof newPassword)
    if (password === newPassword) {
      message.error('请输入不同密码!')
      return
    }

    if (newPassword === '') {
      message.error('密码不能为空!')
      return
    }

    u.changPassword({
      username: username,
      password: newPassword
    },data =>{
      console.log(data)
      message.success('修改成功')
      localStorage.setItem('password', newPassword)
    })
  }

  render(){
    return(
      <div className="pi-content">
        {/* <div className="a-line">
          <span>账户：</span>
          <Input defaultValue="用户名" />
        </div> */}
        <div className="a-line">
          <span>密码：</span>
          <Input type="password" id='user' defaultValue={this.state.oldPassword}/>
        </div>
        <div className="pi-btn" onClick={this.onChangePsd}>修改</div>
      </div>
    );
  }
}